﻿using System;
using System.Xml.Linq;

namespace YArchitech.Temp
{
	public class HYMEPObject : HYObject, IHYXmlSerial
	{
		public HYMEPObject.BranchPlace Place { get; set; }

		public int Index { get; set; }

		public string Name { get; set; }

		public string Number { get; set; }

		public double Length { get; set; }

		public double Velocity { get; set; }

		public double Flow { get; set; }

		public double AddFlow { get; set; }

		public double Roughness { get; set; }

		public virtual string ShowString
		{
			get
			{
				return this.Name + this.Index.ToString();
			}
		}

		public HYMEPObject Previous { get; set; }

		public HYMEPObject Next
		{
			get
			{
				return this.m_NextObject;
			}
		}

		public int NextId
		{
			get
			{
				if (this.m_NextObject != null)
				{
					this.m_NextObjectId = this.m_NextObject.Id;
				}
				return this.m_NextObjectId;
			}
		}

		public bool IsBranchEnd
		{
			get
			{
				return -1 == this.NextId;
			}
		}

		public static HYMEPObject.BranchPlace Down(HYMEPObject.BranchPlace place)
		{
			if (place == HYMEPObject.BranchPlace.Main)
			{
				return HYMEPObject.BranchPlace.Side;
			}
			if (place != HYMEPObject.BranchPlace.Side)
			{
				return HYMEPObject.BranchPlace.SideSide;
			}
			return HYMEPObject.BranchPlace.SideSide;
		}

		public static HYMEPObject.BranchPlace Up(HYMEPObject.BranchPlace place)
		{
			if (place == HYMEPObject.BranchPlace.Side)
			{
				return HYMEPObject.BranchPlace.Main;
			}
			if (place == HYMEPObject.BranchPlace.SideSide)
			{
				return HYMEPObject.BranchPlace.Side;
			}
			return HYMEPObject.BranchPlace.Main;
		}

		public HYMEPObject()
		{
			this.Index = 0;
			this.Place = HYMEPObject.BranchPlace.Main;
			this.Name = "实体";
			this.Previous = null;
			this.m_NextObject = null;
			this.m_NextObjectId = -1;
			this.Number = "";
			this.AddFlow = 0.0;
		}

		public virtual void SetNext(HYMEPObject nextObj)
		{
			if (nextObj == null)
			{
				return;
			}
			this.m_NextObject = nextObj;
			this.m_NextObject.Place = this.Place;
			this.m_NextObjectId = this.m_NextObject.Id;
			this.m_NextObject.Previous = this;
		}

		public virtual HYPipeline GetPreviousPipeline()
		{
			HYMEPObject previous = this.Previous;
			while (previous != null && !(previous is HYPipeline))
			{
				previous = previous.Previous;
			}
			if (previous == null)
			{
				return null;
			}
			return previous as HYPipeline;
		}

		public virtual void RebuildIndex(ref int index)
		{
			if (this.m_NextObject != null)
			{
				this.m_NextObject.RebuildIndex(ref index);
			}
		}

		public virtual void ReAllocateFlow()
		{
			if (this.m_NextObject != null)
			{
				this.m_NextObject.ReAllocateFlow();
				this.Flow = this.m_NextObject.Flow + this.AddFlow;
			}
		}

		public virtual void Cal(HYSystemOption option)
		{
		}

		public override void WriteXml(XElement xe)
		{
			base.WriteXml(xe);
			xe.SetAttributeValue("Index", this.Index);
			xe.SetAttributeValue("Name", this.Name);
			xe.SetAttributeValue("NextObjectId", this.NextId);
			XElement xelement = new XElement(xe.Name);
			this.Next.WriteXml(xelement);
			xe.AddAfterSelf(xelement);
		}

		public override void ReadXml(XElement xe)
		{
			base.ReadXml(xe);
			this.Name = xe.GetStringAttribute("Name", "");
			this.Index = xe.GetIntAttribute("Index", 0);
			this.m_NextObjectId = xe.GetIntAttribute("NextObjectId", 0);
			XElement xelement = xe.NextNode as XElement;
			if (xelement != null)
			{
				this.m_NextObject = HYMEPObject.CreateObject(xelement);
			}
		}

		public static HYMEPObject CreateObject(XElement xeObject)
		{
			return new HYMEPObject();
		}

		public const string XMLNode = "HYMEPObject";

		protected int m_NextObjectId;

		protected HYMEPObject m_NextObject;

		private const int InvalidId = -1;

		public enum BranchPlace
		{
			Main,
			Side,
			SideSide
		}
	}
}
